model{
  for(i in 1:n.obs){  #likelihood
      d.credit[i] ~ dnorm(mu[i], tau.y[i])
      mu[i] <- A[i] + B[i]  #reached WinBUGS limit on num of covariates
      A[i]<- a.unit[country[i]] + b.l.credit[country[i]]*lag.credit[i] + b.l.top1*lag.ineq[i] + b.d.top1*d.ineq[i] +
        b.cl*cum.left[i] + b.l.top1cl*lag.ineq[i]*cum.left[i] + b.d.top1cl*d.ineq[i]*cum.left[i]
      B[i]<-b.l.unemp*lag.unemp[i] + b.d.unemp*d.unemp[i] +
        b.l.pop*lag.pop[i] + b.d.pop*d.pop[i] +
        b.l.gdp*lag.gdp[i] + b.d.gdp*d.gdp[i] +
        b.l.k*lag.gfcf[i] + b.d.k*d.gfcf[i] +
        b.l.cab*lag.cab[i] + b.d.cab*d.cab[i] + 
        b.l.budg*lag.budg[i] + b.d.budg*d.budg[i] +
        b.l.world.save*lag.world.save[i] + b.d.world.save*d.world.save[i] + 
        b.l.bm*lag.bm.growth[i] + b.d.bm*d.bm.growth[i] + 
        b.l.fire*lag.FIRE[i] + b.d.fire*d.FIRE[i] +
        b.l.old*lag.old[i] + b.d.old*d.old[i]   
    lag.credit[i] ~ dnorm(mu.l.credit, tau.l.credit)#imputation models for missing covariates
    lag.ineq[i] ~ dnorm(mu.l.top1, tau.l.top1)
    d.ineq[i] ~ dnorm(mu.d.top1, tau.d.top1)
    lag.budg[i] ~ dnorm(mu.l.budg, tau.l.budg)
    d.budg[i] ~ dnorm(mu.d.budg, tau.d.budg)  
    #lag.cab[i] ~ dnorm(mu.l.cab, tau.l.cab)
    #d.cab[i] ~ dnorm(mu.d.cab, tau.d.cab)
    lag.unemp[i] ~ dnorm(mu.l.unemp, tau.l.unemp)
    d.unemp[i] ~ dnorm(mu.d.unemp, tau.d.unemp)
    #lag.world.save[i] ~ dnorm(mu.l.world.save, tau.l.world.save)
    #d.world.save[i] ~ dnorm(mu.d.world.save, tau.d.world.save)
    lag.bm.growth[i] ~ dnorm(mu.l.bm.growth, tau.l.bm.growth)
    d.bm.growth[i] ~ dnorm(mu.d.bm.growth, tau.d.bm.growth)
    lag.FIRE[i] ~ dnorm(mu.l.fire, tau.l.fire)
    d.FIRE[i] ~ dnorm(mu.d.fire, tau.d.fire)
    logsigma2.y[i] <- g.unit[country[i]] + g.year[years.d[i]] + g.euro*eurozone[i] #model for the variance
    tau.y[i] <- 1/exp(logsigma2.y[i])
    res[i]<-d.credit[i] - mu[i] #residuals
    y.pred[i] ~ dnorm(mu[i], tau.y[i]) #repredicting y  
  }
  
  for(j in 1:(n.countries)){#country RE in model for variance  
    g.unit[j] ~ dnorm(0, tau.vc)
    a.unit[j] ~ dnorm(mu.c, tau.country) #country RE
    #b.l.credit[j] ~ dnorm(mu.ldv, tau.ldv)
    b.l.credit[j] <- -1*temp[j]
    temp[j] ~ dbeta(1,1)
  }
  for(j in 1:n.years){#country RE in model for variance
    g.year[j] ~ dnorm(0, tau.vy) 
  }
  
# priors
#CAR prior for country RE; spatial correlation
  #a.unit[1:n.countries] ~ car.normal(adj[], weights[], num[], tau.country)
  mu.c ~ dnorm(0,0.00001) #country RE
  mu.vc ~ dnorm(0,0.00001) # country RE in variance term
  mu.vy ~ dnorm(0,0.00001) 
  ##imputation means
  mu.l.credit ~ dnorm(0,0.00001) 
  mu.l.top1 ~ dnorm(0,0.00001)
  mu.d.top1 ~ dnorm(0,0.00001)
  mu.l.budg ~ dnorm(0,0.00001)
  mu.d.budg ~ dnorm(0,0.00001)  
  #mu.l.cab ~ dnorm(0,0.00001)
  #mu.d.cab ~ dnorm(0,0.00001)
  mu.l.unemp ~ dnorm(0,0.00001)
  mu.d.unemp ~ dnorm(0,0.00001)
  #mu.l.world.save ~ dnorm(0,0.00001)
  #mu.d.world.save ~ dnorm(0,0.00001)
  mu.l.bm.growth ~ dnorm(0,0.00001)
  mu.d.bm.growth ~ dnorm(0,0.00001)
  mu.l.fire ~ dnorm(0,0.00001)
  mu.d.fire ~ dnorm(0,0.00001)
  ## regression parameters
  #b0 ~ dflat() #reqired by CAR model
  #b.l.credit~ dnorm(0,0.0001)
  b.l.top1~ dnorm(0,0.0001)
  b.d.top1~ dnorm(0,0.0001)
  b.cl~ dnorm(0,0.0001)
  b.l.top1cl~ dnorm(0,0.0001)
  b.d.top1cl~ dnorm(0,0.0001)
  b.l.unemp~ dnorm(0,0.0001)
  b.d.unemp~ dnorm(0,0.0001)
  b.l.pop~ dnorm(0,0.0001)
  b.d.pop~ dnorm(0,0.0001)
  b.l.gdp~ dnorm(0,0.0001)
  b.d.gdp~ dnorm(0,0.0001)
  #b.d.growth~ dnorm(0,0.0001)
  b.l.k~ dnorm(0,0.0001)
  b.d.k~ dnorm(0,0.0001)
  b.l.cab~ dnorm(0,0.0001)
  b.d.cab~ dnorm(0,0.0001)
  b.l.budg~ dnorm(0,0.0001)
  b.d.budg~ dnorm(0,0.0001)
  b.l.world.save~ dnorm(0,0.0001)
  b.d.world.save~ dnorm(0,0.0001)
  b.l.bm~ dnorm(0,0.0001)
  b.d.bm~ dnorm(0,0.0001)
  b.l.fire~ dnorm(0,0.0001)
  b.d.fire~ dnorm(0,0.0001)
  b.l.old~ dnorm(0,0.0001)
  b.d.old~ dnorm(0,0.0001)
  g.euro ~dnorm(0,0.0001)
  ## variances/precisions
  sigma.country ~ dunif(0,50)
  tau.country <- pow(sigma.country,-2)
  sigma.vc ~ dunif(0,10) #variance for country RE in variance term
  sigma.vy ~ dunif(0,10) #variance for year RE in variance term
  sigma.l.credit ~ dunif(0,50)
  tau.l.credit<- pow(sigma.l.credit,-2)
  sigma.l.top1 ~ dunif(0,50)
  sigma.d.top1~ dunif(0,50)
  sigma.l.budg~ dunif(0,50)
  sigma.d.budg~ dunif(0,50)
  #sigma.l.cab~ dunif(0,50)
  #sigma.d.cab~ dunif(0,50)
  sigma.l.unemp~ dunif(0,50)
  sigma.d.unemp~ dunif(0,50)
  #sigma.l.world.save~ dunif(0,50)
  #sigma.d.world.save~ dunif(0,50)
  sigma.l.bm.growth~ dunif(0,50)
  sigma.d.bm.growth~ dunif(0,50)
  sigma.l.fire~ dunif(0,50)
  sigma.d.fire~ dunif(0,50)
  tau.vc <- pow(sigma.vc, -2)
  tau.vy <- pow(sigma.vy, -2)
  tau.l.top1 <- pow(sigma.l.top1,-2)
  tau.d.top1<- pow(sigma.d.top1,-2)
  tau.l.budg<- pow(sigma.l.budg,-2)
  tau.d.budg<- pow(sigma.d.budg,-2)
  #tau.l.cab<- pow(sigma.l.cab,-2)
  #tau.d.cab<- pow(sigma.d.cab,-2)
  tau.l.unemp<- pow(sigma.l.unemp,-2)
  tau.d.unemp<- pow(sigma.d.unemp,-2)
  #tau.l.world.save<- pow(sigma.l.world.save,-2)
  #tau.d.world.save<- pow(sigma.d.world.save,-2)
  tau.l.bm.growth<- pow(sigma.l.bm.growth,-2)
  tau.d.bm.growth<- pow(sigma.d.bm.growth,-2)
  tau.d.fire <- pow(sigma.d.fire,-2)
  tau.l.fire <- pow(sigma.l.fire,-2)
}
